/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jdbc.quanlysach;

//<editor-fold defaultstate="collapsed" desc="1-Thu vien can dung trong JDBC.">
//import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;
//</editor-fold>

/**
 *
 * @author Kim Cuong
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            
            /* Buoc 1 - Dang ky va nap driver. */
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            
            /* Buoc 2 - Tao ket noi den MySQL. */
            String conStr = "jdbc:mysql://localhost:3306/quan_ly_sach";
            Properties info = new Properties();
            info.setProperty("characterEncoding", "utf8");
            info.setProperty("user", "root");
            
            Connection connect = DriverManager.getConnection(conStr, info);
            
            /* Buoc 3 - Menu lua chon chuc nang.*/
            System.out.println("****************Menu Chuc Nang****************");
            System.out.println("*          1 - Them, xoa, sua sach.          *");
            System.out.println("*          2 - Hien thi sach theo the loai.  *");
            System.out.println("**********************************************");
            Scanner scan = new Scanner(System.in);
            System.out.print("-> Chon chuc nang muon thao tac: ");
            int chucNang = Integer.parseInt(scan.nextLine());
            
            switch(chucNang){
                case 1:
                    System.out.println("********************************");
                    System.out.println("*   1 - Them thong tin sach.   *");
                    System.out.println("*   2 - Xoa thong tin sach.    *");
                    System.out.println("*   3 - Sua thong tin sach.    *");
                    System.out.println("********************************");
                    System.out.print("-> Chọn chức năng thao tác: ");
                    int thaoTac = Integer.parseInt(scan.nextLine());
                    
                    switch(thaoTac){
                        case 1:
                            
                    }
                    
                    break;
                case 2:
                    System.out.print("The loai muon tim: ");
                    int theLoai = Integer.parseInt(scan.nextLine());
                    Statement statement = connect.createStatement();
                    String sql = "Select * from Sach Where MaTheLoai=" + theLoai;
                    ResultSet rs = statement.executeQuery(sql);
                    while (rs.next()) {                        
                        String maSach = rs.getString("MaSach");
                        String tenSach = rs.getString("TenSach");
                        double giaBan = rs.getDouble("GiaBan");
                        int soLuong = rs.getInt("SoLuongTon");
                        System.out.println(maSach + " " + tenSach + " " + giaBan + " " + soLuong);
                    }
                    break;
            }
                        
            /* Buoc cuoi - Dong ket noi MySQL. */
            connect.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    //<editor-fold defaultstate="collapsed" desc="1-Thong tin cua sach muon them.">
    public static void thongTinSachThem(){
        
    }
    //</editor-fold>
        
}
